package com.shopping.dao.Impl;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import com.shopping.dao.IBaseDao;
import com.shopping.model.Goods;
@Repository
/**
 * 商品表的增删改查
 * @author 吴飞
 *
 */
public interface IGoodsDao extends IBaseDao<Goods>{

	@Override
	@Select("select * from goods")
	List<Goods> selectAll();
	
	@Select("select * from goods where id=#{goodId}")
	Goods selectById(@Param("goodId")Integer goodsId);
	
	
	@Override
	@Select("select * from goods order by sales desc limit #{m},#{n}")
	List<Goods> selectByPage(@Param("m")Integer offset,@Param("n")Integer rows);

	@Override
	@Insert("insert into goods values(null,#{cover},#{name},#{intro},#{spec},#{price},#{stock},#{sales},#{content},#{type_id})")
	Integer insert(Goods entity);

	@Override
	@Delete("delete from goods where id=#{id}")
	Integer deleteById(@Param("id")Integer id);

	@Override
	@Update("update goods set cover=#{cover},name=#{name},intro=#{intro},spec=#{spec},price=#{price},stock=#{stock},content=#{content},type_id=#{typeId} where id=#{id}")
	Integer update(Goods model);

	/**
	 * 减少库存
	 * @param id
	 * @param amount
	 */
    @Update("update goods set stock=stock-#{amount} where id = #{id}")
	void updateStock(@Param("id") int id, @Param("amount") int amount);

    /**
     * 增加销量
     * @param id
     * @param amount
     */
    @Update("update goods set sales=sales+#{amount} where id = #{id}")
	void updateSales(@Param("id") int id, @Param("amount") int amount);
}
